package com.hxd.otc.module.my.fragment;

import android.view.Gravity;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseRecyclerFragment;
import com.hxd.otc.common.enums.TradeType;
import com.hxd.otc.dialog.ForbiddenBankCardDialog;
import com.hxd.otc.event.Event;
import com.hxd.otc.module.my.adapter.AdAdapter;
import com.hxd.otc.module.my.bean.AdvertBean;
import com.hxd.otc.module.my.contract.AdContract;
import com.hxd.otc.module.my.presenter.AdPresenter;
import com.hxd.otc.utils.LogUtils;
import com.hxd.otc.utils.UIUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;


/**
 * Created by lzy on 2018/6/11 0011.
 * description ${广告管理-广告列表}
 */
public class AdFragment extends BaseRecyclerFragment<AdPresenter, AdAdapter, AdvertBean> implements AdContract.View, AdAdapter.OnRevokeAdvertListener {

    public static final String EXTRA_TRADE_TYPE = "trade_type";

    private ForbiddenBankCardDialog<String> mRevokeAdvertDialog;

    @Override
    protected void initView() {
        registerEventBus();
        mPresenter.setExType((TradeType) getArguments().getSerializable(EXTRA_TRADE_TYPE));
        super.initView();
    }

    @Override
    protected AdAdapter createAdapter() {
        AdAdapter adAdapter = new AdAdapter(getContext());
        adAdapter.setOnRevokeAdvertListener(this);
        return adAdapter;
    }

    @Override
    protected AdPresenter createPresenter() {
        return new AdPresenter(this);
    }

    int pos;

    @Override
    public void onRevoke(int position) {
        if (mRevokeAdvertDialog == null)
            mRevokeAdvertDialog = createRevokeAdvertDialog();
        pos = position;
        mRevokeAdvertDialog.setData(mAdapter.getItem(position).getId()).show();
    }

    private ForbiddenBankCardDialog<String> createRevokeAdvertDialog() {
        return new ForbiddenBankCardDialog<String>(getContext())
                .setTitle(getString(R.string.hint))
                .setMessageGravity(Gravity.CENTER)
                .setMessage(getString(R.string.is_revoke_advert))
                .setOnConfirmClickListener(new ForbiddenBankCardDialog.OnConfirmClickListener<String>() {
                    @Override
                    public void onConfirm(String advertId) {
                        mPresenter.revokeAdvert(advertId, mAdapter.getItem(pos).isNosenseOrder());
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        UIUtils.dismissDialog(mRevokeAdvertDialog);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateAccountInfoEvent(Event.OnPublishAdvertEvent publishAdvertEvent) {
        if (mPresenter.getExType().equals(publishAdvertEvent.getExType())) {
            mPresenter.onRefresh();
        }
    }

    @Override
    public void revokeAdvertSuccess(String id) {
        List<AdvertBean> items = mAdapter.getItems();
        for (int i = 0; i < items.size(); i++) {
            AdvertBean advertBean = items.get(i);
            if (id.equals(advertBean.getId())) {
                advertBean.setStatus(9);
                mAdapter.notifyItemChanged(i);
                break;
            }
        }
    }
}
